package com.zbkj.service.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zbkj.common.model.project.EstimateFun;
import com.zbkj.common.vo.CountVo;
import com.zbkj.common.vo.project.EstimateFunSortNumVo;
import com.zbkj.service.dao.project.EstimateFunMapper;
import com.zbkj.service.service.project.EstimateFunService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 估算功能描述表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-09-13
 */
@Service
public class EstimateFunServiceImpl extends ServiceImpl<EstimateFunMapper, EstimateFun> implements EstimateFunService {

    @Resource
    private EstimateFunMapper estimateFunMapper;

    @Override
    public List<EstimateFun> getValidFunDetailList(Long projectId,Integer neModifyState, String reqNo) {
        if(Objects.isNull(projectId)) {
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<EstimateFun> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EstimateFun::getDeleteFlag, 0);
        wrapper.eq(EstimateFun::getFunStatus, 0);
        wrapper.eq(EstimateFun::getProjectId, projectId);
        if(StringUtils.isNotBlank(reqNo)) {
            wrapper.eq(EstimateFun::getReqNo, reqNo);
        }
        if(Objects.nonNull(neModifyState)) {
            wrapper.ne(EstimateFun::getModifyState, neModifyState);
        }
        wrapper.last(" order by idx asc, id asc");
        return this.list(wrapper);
    }
    /*@Override
    public void saveEstimateFun(EstimateFun estimateFun) {
        if(Objects.isNull(estimateFun) || Objects.isNull(estimateFun.getProjectId())) {
            return;
        }
        if(Objects.isNull(estimateFun.getSortNum())) {
            Integer maxSortNum = estimateFunMapper.getMaxSortNumByProductId(estimateFun.getProjectId());
            maxSortNum = Objects.isNull(maxSortNum) ? 0 : maxSortNum;
            estimateFun.setSortNum(maxSortNum+1);
        }else {
            //更新db sortNum >=当前排序号的记录sortNum统一加1
            this.updateIncrSortNum(estimateFun.getProjectId(),estimateFun.getSortNum());
        }
        this.save(estimateFun);
    }*/

    @Override
    public void saveEstimateFunList(List<EstimateFun> funDetailList) {
        if(CollectionUtils.isEmpty(funDetailList)) {
            return;
        }
        this.saveBatch(funDetailList);
    }

    /*@Override
    public boolean updateEstimateFun(EstimateFun estimateFun) {
        if(Objects.isNull(estimateFun) || Objects.isNull(estimateFun.getProjectId())
                || Objects.isNull(estimateFun.getId())) {
            return false;
        }
        LambdaUpdateWrapper<EstimateFun> updateWrappr = new LambdaUpdateWrapper<>();
        updateWrappr.eq(EstimateFun::getDeleteFlag, 0)
                .eq(EstimateFun::getProjectId,estimateFun.getProjectId())
                .eq(EstimateFun::getId,estimateFun.getId());
        return this.update(estimateFun,updateWrappr);
    }*/

    @Override
    public void updateEstimateFunList(List<EstimateFun> funDetailList) {
        if(CollectionUtils.isEmpty(funDetailList)) {
            return;
        }
        this.updateBatchById(funDetailList);
    }

    /*@Override
    public boolean modifyStatusDel(Long projectId, Long id) {
        if(Objects.isNull(projectId) || Objects.isNull(id)) {
            return false;
        }
        LambdaUpdateWrapper<EstimateFun> updateWrappr = new LambdaUpdateWrapper<>();
        updateWrappr.set(EstimateFun::getModifyState,3)
                .eq(EstimateFun::getDeleteFlag, 0)
                .eq(EstimateFun::getProjectId,projectId)
                .eq(EstimateFun::getId,id);
        return this.update(updateWrappr);
    }*/

    @Override
    public void modifyStatusDel(Long projectId, List<Long> idList) {
        if(CollectionUtils.isEmpty(idList)) {
            return;
        }
        LambdaUpdateWrapper<EstimateFun> updateWrappr = new LambdaUpdateWrapper<>();
        updateWrappr.set(EstimateFun::getModifyState,3)
                .eq(EstimateFun::getDeleteFlag, 0)
                .eq(EstimateFun::getProjectId,projectId)
                .in(EstimateFun::getId,idList);
        this.update(updateWrappr);
    }

    @Override
    public void restSortNum(List<Long> idList) {
        if(CollectionUtils.isEmpty(idList)) {
            return;
        }
        List<EstimateFun> funList = Lists.newArrayList();
        int sortNum = 0;
        for(Long id : idList) {
            EstimateFun fun = new EstimateFun();
            fun.setId(id);
            sortNum += 1;
            fun.setSortNum(sortNum);
            funList.add(fun);
        }
        this.updateBatchById(funList);
    }

    @Override
    public void updateStatusToHis(Long projectId) {
        if(Objects.isNull(projectId)) {
            return;
        }
        LambdaUpdateWrapper<EstimateFun> updateWrappr = new LambdaUpdateWrapper<>();
        updateWrappr.set(EstimateFun::getFunStatus,1)
                .eq(EstimateFun::getDeleteFlag, 0)
                .eq(EstimateFun::getFunStatus, 0)
                .eq(EstimateFun::getProjectId,projectId);
        this.update(updateWrappr);
    }

    @Override
    public boolean delByProjectId(Long projectId) {
        if(Objects.isNull(projectId)) {
            return false;
        }
        LambdaUpdateWrapper<EstimateFun> updateWrappr = new LambdaUpdateWrapper<>();
        updateWrappr.set(EstimateFun::getDeleteFlag,1)
                .eq(EstimateFun::getDeleteFlag, 0)
                .eq(EstimateFun::getProjectId,projectId);
        return  this.update(updateWrappr);
    }

    @Override
    public void updateIncrSortNum(Long projectId, Integer minSort) {
        if(Objects.isNull(projectId) || Objects.isNull(minSort)) {
            return;
        }
        LambdaUpdateWrapper<EstimateFun> updateWrappr = new LambdaUpdateWrapper<>();
        updateWrappr.setSql("sort_num = sort_num + 1")
                .eq(EstimateFun::getDeleteFlag, 0)
                .eq(EstimateFun::getFunStatus, 0)
                .ne(EstimateFun::getModifyState, 3)
                .ge(EstimateFun::getSortNum, minSort)
                .eq(EstimateFun::getProjectId,projectId);
        this.update(updateWrappr);
    }

    @Override
    public List<EstimateFunSortNumVo> getModifyFunSortNumList(Long projectId, Integer newSortNum) {
        List<EstimateFunSortNumVo> numVoList = Lists.newArrayList();
        if(Objects.isNull(projectId) || Objects.isNull(newSortNum)) {
            return numVoList;
        }
        LambdaQueryWrapper<EstimateFun> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(EstimateFun::getId,EstimateFun::getSortNum);
        wrapper.eq(EstimateFun::getDeleteFlag, 0);
        wrapper.eq(EstimateFun::getFunStatus, 0);
        wrapper.eq(EstimateFun::getProjectId, projectId);
        wrapper.gt(EstimateFun::getSortNum, newSortNum);
        wrapper.ne(EstimateFun::getModifyState, 3);
        List<EstimateFun> funList = this.list(wrapper);
        for(EstimateFun fun : funList) {
            EstimateFunSortNumVo vo = new EstimateFunSortNumVo();
            vo.setId(fun.getId());
            vo.setSortNum(fun.getSortNum());
            numVoList.add(vo);
        }
        return numVoList;
    }

    @Override
    public BigDecimal sumFunPointsByProjectId(long projectId) {
        BigDecimal sumPoints = estimateFunMapper.sumFunPointsByProjectId(projectId);
        return Objects.isNull(sumPoints) ? null : sumPoints.setScale(2, RoundingMode.HALF_UP);
    }

    @Override
    public Map<Integer, Integer> findIdxCount(Long projectId) {
        List<CountVo> countList = estimateFunMapper.findIdxCount(projectId);
        return countList.stream().collect(Collectors.toMap(k->k.getIdx(),v->v.getCountVal(),(ov,nv)->nv));
    }

    @Override
    public List<EstimateFun> getListByProjectId(Long projectId) {
        return this.lambdaQuery().eq(EstimateFun::getDeleteFlag, 0)
                .eq(EstimateFun::getProjectId, projectId)
                .list();
    }
}
